<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
<link rel="stylesheet" href="../style/journal.css" type="text/css" />
<style type="text/css"><!--
.googleadsense {
	margin: 2px;
	padding: 0px;
//--></style><script src="http://www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-65008-1";
urchinTracker();
</script><title>如何寻找用户真实IP</title>
</head>
<body>
<a href="index.html">Journal</a>(2005) | <a href="../blog/"><b>Blog</b></a>(2006) | <a href="http://www.fayland.org/cgi-bin/random_link.pl">RandomLink</a> | <a href="AboutFayland.html">WhoAmI</a> | <a href="LiveBookmark.html">LiveBookmark</a> | <a href="http://www.fayland.org/">HomePage</a>
<p><&lt;Previous: <a href="Gallery.html">Gallery缩略图制作</a>&nbsp;&nbsp;>>Next: <a href="Kwiki.html">Kwiki 安装笔记</a></p>
<h1>如何寻找用户真实IP</h1>
<div class='content'>
<p>Category: <a href='Basic.html'>Basic</a> &nbsp; Keywords: <b>REMOTE_ADDR IP</b></p><h2>Generally</h2>
一般程序代码所书写的都是这样（如 <a href='http://www.ikonboard.com'>ikonboard</a>, <a href='http://www.infopop.com'>UBB</a>, UltraThreads etc.）：
<pre>
$IP_ADDRESS = $ENV{'HTTP_X_FORWARDED_FOR'} || $ENV{'REMOTE_ADDR'};
</pre>
<ul>这里有三种情况：
<li>通过<b>透明</b>代理服务器，那$ENV{'HTTP_X_FORWARDED_FOR'}为此代理服务器地址，而$ENV{'REMOTE_ADDR'}为你的地址。</li>
<li>通过<b>匿名</b>代理服务器，同样$ENV{'HTTP_X_FORWARDED_FOR'}为此代理服务器地址，而$ENV{'REMOTE_ADDR'}为此代理服务器连接网页的地址（可与代理服务器地址不同）。 </li>
<li>没有使用代理服务器，那$ENV{'HTTP_X_FORWARDED_FOR'}为空，而$ENV{'REMOTE_ADDR'}为用户真实ip</li>
</ul>
举个例子会清晰点：
<pre>
#proxy(anonymous) is 80.59.189.28,then got
$ENV{'REMOTE_ADDR'} = '80.58.3.235';
$ENV{'HTTP_X_FORWARDED_FOR'} = '80.59.189.28';
#no proxy
$ENV{'REMOTE_ADDR'} = '211.90.227.119';#my temp true ip
$ENV{'HTTP_X_FORWARDED_FOR'} = '';
</pre>
<h2>Advance</h2>
其实另外还有两个ENV环境变量：'HTTP_CLIENT_IP', 'X_CLIENT_IP'。不过不太常用。如果增加此两变量，代码如下，前三者顺序可能有变（如 <a href='http://www.yabbforum.com'>YaBB</a>, <a href='http://www.codingdomain.com'>X-Forum</a> etc.）：
<pre>
$IP_ADDRESS = $ENV{'HTTP_X_FORWARDED_FOR'} || $ENV{'HTTP_CLIENT_IP'} || $ENV{'X_CLIENT_IP'} || $ENV{'REMOTE_ADDR'};
</pre>
<h2>Especial</h2>
不适用于localhost/127.0.0.1<br>
对于匿名/Anonymous Proxy来说，因为它不会将使用Proxy的人的ip一起发请求过来，所以获得使用Proxy人的ip是impossible mission.<br>
获得Proxy相关信息的环境变量是$ENV{'HTTP_VIA'}.<p></div>
<p><&lt;Previous: <a href="Gallery.html">Gallery缩略图制作</a>&nbsp;&nbsp;>>Next: <a href="Kwiki.html">Kwiki 安装笔记</a></p>
<p><strong>Options:</strong> <a href='http://del.icio.us/post?title=%E5%A6%82%E4%BD%95%E5%AF%BB%E6%89%BE%E7%94%A8%E6%88%B7%E7%9C%9F%E5%AE%9EIP&url=http://www.fayland.org/journal/IPaddress.html'>+Del.icio.us</a></p>
<strong>Related items</strong>
<ul><li><a href='JS_encode.html'>Ajax && encodeURIComponent</a> < <span class='digit'>2005-05-15 15:31:00</span> ></li><li><a href='050320.html'>A trip to the Yellow Mountain</a> < <span class='digit'>2005-03-20 23:11:43</span> ></li><li><a href='JS_event.html'>Javascript: Events</a> < <span class='digit'>2005-05-10 23:36:36</span> ></li><li><a href='PBP2.html'>Tips from Perl Best Practices</a> < <span class='digit'>2005-09-13 16:57:52</span> ></li><li><a href='PBP3.html'>Tips from Perl Best Practices, Part 2</a> < <span class='digit'>2005-09-22 00:29:34</span> ></li><li><a href='AutoSave.html'>AutoSave / 自动存储</a> < <span class='digit'>2005-10-14 01:26:10</span> ></li></ul>
Created on <span class="digit">2004-10-30 19:05:35</span>, Last modified on <span class="digit">2005-01-15 22:39:57</span><br />
Copyright 2004-2005 All Rights Reserved. Powered by <a href="Eplanet.html">Eplanet</a> && <a href='http://catalyst.perl.org'>Catalyst</a> 5.62.
</body>
</html>